#set($layout = "blank.html")
<div class="space-4"></div>
<div class="row">
	<div class="col-xs-1"></div>
	<div class="col-xs-10">
		<form id="addPermissionForm">
			<div class="space-10"></div>
			<div>
				<label> 权限名 <small class="text-info">设置权限名</small></label>
				<div>
					<input class="form-control" type="text" placeholder="请输入权限名" name="name" data-type='required' data-error='请输入权限名' onchange="checkPermissionName(this.value)" />
				</div>
			</div>
			<div class="space-4"></div>
			<div>
				<label>是否菜单<small class="text-info">关于设置菜单标示</small></label>
				<div>
					<select class="form-control" name="isMenu" onchange="showUrl(this.value)">
						<option value="-99">请选择是否是菜单</option>
						<option value="true">是</option>
						<option value="false" selected>否</option>
					</select>
				</div>
			</div>
			<div class="space-4"></div>
			<div>
				<label>权限分组路径<small class="text-info">权限属于哪个分组</small></label>
				<div>
					<select class="form-control" name="menuGroup">
						<option value="-99">请选择权限路径分组</option> #foreach($permissionGroup in ${obj.data.permissionGroup})
						<option value="$!{permissionGroup}">$!{permissionGroup.name}</option> #end
					</select>
				</div>
			</div>
			<div class="space-4"></div>
			<div>
				<label>菜单高亮关键字<small class="text-info">例如:user_list</small></label>
				<div>
					<input class="form-control" type="text" placeholder="请输入菜单高亮关键字" name="hilightKey" data-type='required' data-error='请输入菜单高亮关键字'>
				</div>
			</div>
			<div class="space-4"></div>
			<div id="urlDiv">
				<label>权限url<small class="text-info">例如:/user/list</small></label>
				<div>
					<input class="form-control" type="text" placeholder="请输入url" name="url" data-type="reg" data-reg="/^[\/\w*]*$/" data-default="/a/b" data-error="请输入正确的权限url"
						onblur="checkUrl(this.value)">
				</div>
			</div>
			<div class="space-4"></div>
			<div>
				<label>描述<small class="text-info">补充说明</small></label>
				<div>
					<input class="form-control" type="text" placeholder="请输入描述" name="description" data-type='required' data-error='请输入描述'>
				</div>
			</div>
			<div class="space-12"></div>
			<div class="pull-right" style="margin-right:45px">
				<button type="button" class="btn btn-sm btn-primary" onclick="save()">
					<i class="fa fa-save"></i> 提交
				</button>
				<button class="btn btn-sm btn-warning btn-dialog-undo">
					<i class="fa fa-undo"></i> 返回
				</button>
			</div>
		</form>
	</div>
	<div class="col-xs-1"></div>
</div>

<script>
	function save() {
		if ($('input').validation()) {
			var url = $('input[name=url]').val();
			var name = $('input[name=name]').val();
			var isMenu = $('select[name=isMenu]').val();
			if (isMenu == "true") {
				if (!checkUrl(url)) {
					return;
				}
			}
			if ($('select[name=menuGroup]').val() == -99) {
				showMessage("请选择权限路径分组");
				return;
			}
			if ($('select[name=isMenu]').val() == -99) {
				showMessage("请选择是否是菜单");
				return;
			}
			if (checkPermissionName(name)) {
				$.post(getRootPath() + '/permission/add', $('#addPermissionForm').serialize(), commonAjaxCallBasck, 'json');
			}
		}

	}
	//检查权限名是否存在
	function checkPermissionName(name) {
		if (name == null || name == "") {
			showMessage("权限名不能为空");
			return false;
		}
		var r;
		$.ajax({
			url : getRootPath() + "/permission/checkPermissionName",
			async : false,
			type : "post",
			dataType : "json",
			data : {
				name : name
			},
			success : function(result) {
				if (result.operationState == "SUCCESS") {
					r = true;
				} else {
					r = false;
					showMessage(result.data.reason);
				}
			}
		});
		return r;
	}
	function checkUrl(url) {
		console.log(url);
		if (url == null || url == "") {
			showMessage("权限url不能为空");
			return false;
		}
		var r;
		$.ajax({
			url : getRootPath() + '/permission/checkUrl',
			async : false,
			type : "post",
			dataType : "json",
			data : {
				url : url
			},
			success : function(result) {
				if (result.operationState == "SUCCESS") {
					r = true;
				} else {
					r = false;
					showMessage(result.data.reason);
				}
			}
		});
		return r;
	}
	function showUrl(value) {
		if (value == "true") {
			$("#urlDiv").show();
		} else {
			$("#urlDiv").hide();
			$("input[name=url]").val("");
		}
	}
</script>